import React from 'react';
import {Spin, Card, Button, Table, Row, Col} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import PropTypes from 'prop-types';
import {dutyUnitStatus, isYorN} from '../Enum';
import routes from './routes';
import {TABLE} from '../constants';
import {conventEnumValueToString} from '../utils';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import {getDetail, stopData, startData} from './api';
import {injectIntl} from './intl';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import styles from './style.css';
const {Description} = DescriptionList;
class DetailPanel extends React.PureComponent {
    state = {
        isFetching: false,
        data: {},
        details: []
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({isFetching: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        isFetching: false,
                        data: Object.assign({}, res.data, {
                            // eslint-disable-next-line no-nested-ternary
                            isSecondClaim: (res.data.isSecondClaim === undefined || res.data.isSecondClaim === null) ? undefined : (res.data.isSecondClaim ? 1 : 2),
                            // eslint-disable-next-line no-nested-ternary
                            isInsideShare: (res.data.isInsideShare === undefined || res.data.isInsideShare === null) ? undefined : (res.data.isInsideShare ? 1 : 2)
                        }),
                        details: res.data.details
                    });
                else
                    this.setState(({
                        isFetching: false
                    }));
            });
    }
    onClickStartBtn = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.state.data.rowVersion
        };
        startData(this.state.data.id, data)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    onClickStopBtn = () => {
        this.setState({loading: true});
        const data = {
            rowVersion: this.state.data.rowVersion
        };
        stopData(this.state.data.id, data)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    onCancel = () => {
        this.props.history.push(routes.query.url());
    }
    render() {
        const {data} = this.state;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(data.options, PERMISSION.update))
            operations.push(<Button key="update" href={`#${routes.update.format(this.props.id)}`} size="small">{
                formatMessage({
                    id: 'detailPanel.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.start) && hasPermissions(data.options, PERMISSION.start))
            operations.push(<WrappedPopconfirm
                key="start"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickStartBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.start',
                    defaultMessage: '是否确认启用？'
                })}>
                <Button key="start" size="small">{
                    formatMessage({
                        id: 'detailPanel.start',
                        defaultMessage: '启用'
                    })
                }</Button>
            </WrappedPopconfirm>);
        if(hasPermissions(this.props.permissions, PERMISSION.stop) && hasPermissions(data.options, PERMISSION.stop))
            operations.push(<WrappedPopconfirm
                key="stop"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickStopBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.stop',
                    defaultMessage: '是否确认停用？'
                })}>
                <Button key="stop" size="small">{
                    formatMessage({
                        id: 'detailPanel.stop',
                        defaultMessage: '停用'
                    })
                }</Button>
            </WrappedPopconfirm>);
        const colums = [{
            title: formatMessage({
                id: 'detailPanel.column.personnelName',
                defaultMessage: '姓名'
            }),
            dataIndex: 'personnelName',
        }];
        return (
            <div>
                <Spin spinning={this.state.isFetching}>
                    <Card>
                        <DescriptionList title={<Row><Col span={6}>{
                            formatMessage({
                                id: 'detailPanel.card.dutyUnit',
                                defaultMessage: '责任单位'
                            })
                        }</Col>
                        <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col>
                        </Row>}>
                            {!this.props.isDealerUser &&
                                <Description term={formatMessage({
                                    id: 'detail.operation.brandCode',
                                    defaultMessage: '品牌编号'
                                })}>{data.brandCode}
                                </Description>
                            }
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.code',
                                defaultMessage: '责任单位编号'
                            })}>{data.code}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.name',
                                defaultMessage: '责任单位名称'
                            })}>{data.name}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.status',
                                defaultMessage: '状态'
                            })}>{conventEnumValueToString(dutyUnitStatus, data.status)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.isSecondClaim',
                                defaultMessage: '是否生成二次索赔'
                            })}>{conventEnumValueToString(isYorN, data.isSecondClaim)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.isInsideShare',
                                defaultMessage: '是否生成内部分摊'
                            })}>{conventEnumValueToString(isYorN, data.isSecondClaim)}
                            </Description>
                            <Description term={formatMessage({
                                id: 'detailPanel.operation.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}
                            </Description>
                        </DescriptionList>
                    </Card>
                    <Card>
                        <DescriptionList title={formatMessage({
                            id: 'detailPanel.table.details',
                            defaultMessage: '清单'
                        })}>
                            <Table
                                className="white-space-nowrap"
                                columns={colums}
                                rowKey="personnelId"
                                pagination={false}
                                dataSource={this.state.details}
                                {...TABLE} />
                        </DescriptionList>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button type="primary" onClick={this.onCancel}>{this.props.intl.formatMessage({
                        id: 'detailPanel.operation.onCancel',
                        defaultMessage: '返回'
                    })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    permissions: PropTypes.array,
    successReturn: PropTypes.func
};

import {connect} from 'react-redux';
import {tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    permissions: getPermissions(state)
});
const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
